package com.canaan.business.controller.pc.basic;

import java.util.List;

import com.canaan.business.domain.basic.ContainerBarcode;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.BarcodeVo;
import com.canaan.business.service.basic.IContainerBarcodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.print.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;

/**
 * WMS 容器绑定条码Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@Api(tags = "WMS 容器绑定条码")
@RestController
@RequestMapping("/basic/barcode")
public class ContainerBarcodeController extends BaseController {
    @Resource
    private IContainerBarcodeService containerBarcodeService;

    public static void main(String[] args) {
        // 查找所有可用的打印服务
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

        for (PrintService printService : printServices) {
            System.out.println("可用打印机: " + printService.getName());
        }

        if (printServices.length > 0) {
            try {
                // 选择第一个打印服务
                PrintService defaultService = printServices[0];
                DocPrintJob printJob = defaultService.createPrintJob();
                String contentToPrint = "Hello, Java Print Service!";
//                DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
                DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                Doc doc = new SimpleDoc(contentToPrint, flavor, null);

//                String text = "Hello, World!";
//                byte[] labelBytes = text.getBytes();
//                DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
//                Doc doc = new SimpleDoc(labelBytes, flavor, null);

                System.out.println("Print job flavor: " + flavor);
                // 执行打印
                printJob.print(doc, null);
                System.out.println("打印作业已发送到: " + defaultService.getName());
            } catch (PrintException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("没有找到可用的打印服务！");
        }
    }
    /**
     * 查询WMS 容器绑定条码列表
     */
    @ApiOperation(value = "查询WMS 容器绑定条码列表")
    @GetMapping("/list")
    public TableDataInfo list(ContainerBarcode containerBarcode) {
        startPage();
        List<ContainerBarcode> list = containerBarcodeService.selectContainerBarcodeList(containerBarcode);
        return getDataTable(list);
    }

    /**
     * 导出WMS 容器绑定条码列表
     */
    @ApiOperation(value = "导出WMS 容器绑定条码列表")
    @Log(title = "WMS 容器绑定条码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContainerBarcode containerBarcode) {
        List<ContainerBarcode> list = containerBarcodeService.selectContainerBarcodeList(containerBarcode);
        ExcelUtil<ContainerBarcode> util = new ExcelUtil<ContainerBarcode>(ContainerBarcode.class);
        util.exportExcel(response, list, "WMS 容器绑定条码数据");
    }

    /**
     * 获取WMS 容器绑定条码详细信息
     */
    @ApiOperation(value = "获取WMS 容器绑定条码详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(containerBarcodeService.selectContainerBarcodeById(id));
    }

    /**
     * 根据容器编码获取入库条码信息
     */
    @ApiOperation(value = "根据容器编码获取条码")
    @GetMapping(value = "/selectInStockBarcodeByContainerNo/{containerNo}")
    public AjaxResult selectInStockBarcodeByContainerNo(@PathVariable("containerNo") String containerNo){
        List<BarcodeVo> barcodeVoList =  this.containerBarcodeService.selectInStockBarCodeByContainerNo(containerNo);
        return success(barcodeVoList);
    }
    /**
     * 根据容器编码获取出库的库存条码信息
     */
    @ApiOperation(value = "根据容器编码获取条码")
    @GetMapping(value = "/selectOutStockBarcodeByContainerNo/{containerNo}")
    public  AjaxResult selectOutStockBarcodeByContainerNo(@PathVariable("containerNo") String containerNo){
        List<WmsStorehouseStock> barcodeVoList =  this.containerBarcodeService.selectOutStockBarCodeByContainerNo(containerNo);
        return success(barcodeVoList);
    }

    /**
     * 根据容器编号或条码号查询容器信息
     */
    @ApiOperation(value = "根据容器编号或条码号查询容器信息")
    @GetMapping(value = "/selectByContainerNoOrBarcode/{containerNoOrBarcode}")
    public  AjaxResult selectByContainerNoOrBarcode(@PathVariable("containerNoOrBarcode") String containerNoOrBarcode){
        return success(this.containerBarcodeService.selectByContainerNoOrBarcode(containerNoOrBarcode));
    }

    /**
     * 新增WMS 容器绑定条码
     */
    @ApiOperation(value = "新增WMS 容器绑定条码")
    @Log(title = "WMS 容器绑定条码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContainerBarcode containerBarcode) {
        return toAjax(containerBarcodeService.insertContainerBarcode(containerBarcode));
    }

    /**
     * 修改WMS 容器绑定条码
     */
    @ApiOperation(value = "修改WMS 容器绑定条码")
    @Log(title = "WMS 容器绑定条码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContainerBarcode containerBarcode) {
        return toAjax(containerBarcodeService.updateContainerBarcode(containerBarcode));
    }

    /**
     * 删除WMS 容器绑定条码
     */
    @ApiOperation(value = "删除WMS 容器绑定条码")
    @Log(title = "WMS 容器绑定条码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(containerBarcodeService.deleteContainerBarcodeByIds(ids));
    }

    /**
     * 批量新增WMS 容器绑定条码
     */
    @ApiOperation(value = "批量新增WMS 容器绑定条码")
    @Log(title = "批量新增WMS 容器绑定条码", businessType = BusinessType.INSERT)
    @PostMapping("/batchSave")
    public AjaxResult batchUpdateContainerBarcode(@RequestBody List<ContainerBarcode> containerBarcodeList) {
        return toAjax(containerBarcodeService.batchUpdateContainerBarcode(containerBarcodeList));
    }
}
